package cn.exrick.xboot.modules.AccessControl.serviceimpl;

import cn.exrick.xboot.common.utils.ThreadPoolUtil;
import cn.exrick.xboot.modules.AccessControl.AccessControlUserDto.AcForm;
import cn.exrick.xboot.modules.AccessControl.AccessControlUserDto.AcUserForm;
import cn.exrick.xboot.modules.AccessControl.AccessControlUserDto.Acdto;
import cn.exrick.xboot.modules.AccessControl.AccessControlUserDto.UserDto;
import cn.exrick.xboot.modules.AccessControl.dao.mapper.AccessControlUserMapper;
import cn.exrick.xboot.modules.AccessControl.entity.AccessControlUser;
import cn.exrick.xboot.modules.AccessControl.service.IAccessControlUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * AccessControlUser接口实现
 * @author portal
 */
@Slf4j
@Service
@Transactional
public class IAccessControlUserServiceImpl extends ServiceImpl<AccessControlUserMapper, AccessControlUser> implements IAccessControlUserService {

    @Autowired
    private AccessControlUserMapper accessControlUserMapper;

    /**
     * 连表查询获取用户（user表与内外网权限中间表），查询出没有在中间表的user信息
     * @return
     */
    @Override
    public Page<UserDto> findUserByAccessControl(Page page, AcUserForm user){
        return accessControlUserMapper.findUserByAccessControl(page,user);
    }
    /**
     * 连表查询获取用户（user表与内外网权限中间表），查询出在中间表的user信息
     * @return
     */
    @Override
    public Page<Acdto> findAccessControl(Page page, AcUserForm user){
        return accessControlUserMapper.findAccessControl(page,user);
    }
    /**
     * 删除acu数据
     * @param accessControlUserList
     */
    @Override
    public void delAccessControlBySys(List<AccessControlUser> accessControlUserList){
        ThreadPoolUtil.getPool().execute(new delAcuThread(accessControlUserList));
    }

    /**
     * 通过username 和sysid 查询acu
     * @return
     */
    @Override
    public List<AccessControlUser> findAcuBySysAndUser(AccessControlUser accessControlUser){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sys_id",accessControlUser.getSysId());
        queryWrapper.eq("username",accessControlUser.getUsername());
        List<AccessControlUser> list = accessControlUserMapper.selectList(queryWrapper);
        return list;
    }









    /**
     * 删除acu线程
     */
    private class delAcuThread implements Runnable {
            private List<AccessControlUser> accessControlUserList;
            public delAcuThread(List<AccessControlUser> accessControlUserList){
            this.accessControlUserList=accessControlUserList;
        }
        @Override
        public void run() {
            for (AccessControlUser accessControlUser : accessControlUserList) {
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("sys_id",accessControlUser.getSysId());
                int delete = IAccessControlUserServiceImpl.this.accessControlUserMapper.delete(queryWrapper);
            }

        }
    }

}